// Eric McDonald1
// Tic Tac Tioe with hopefully working AI

// header space
#include <iostream>
using namespace std;

// Global variable declaration
char boardPosition1 = '1';
char boardPosition2 = '2';
char boardPosition3 = '3';
char boardPosition4 = '4';
char boardPosition5 = '5';
char boardPosition6 = '6';
char boardPosition7 = '7';
char boardPosition8 = '8';
char boardPosition9 = '9';
bool GameOver = false;
bool GameWin = true;



// Class that draws the board
class Board
{
public:
	void drawBoard ()
	{
	cout << "  " << boardPosition1 << "  |  " << boardPosition2 << "  |  " << boardPosition3 << endl;
	cout << "_________________" << endl;
	cout << "  " << boardPosition4 << "  |  " << boardPosition5 << "  |  " << boardPosition6 << endl;
	cout << "_________________" << endl;
	cout << "  " << boardPosition7 << "  |  " << boardPosition8 << "  |  " << boardPosition9 << endl;
	}
};

// Class that controls the AI driven second player. 
// Current bugs: When X is played in middle first, AI seems to skip its turn.
// When game ends on AI's turn, it asks for your turn again before ending. May be stuck in an Invalid input loop. 
class secPlayer
{
public:
	int pMove2 (int pTurn)
	{

		// Counter creation to prevent computer going multiple times in a turn
		int count = 0;
		
		// If player goes first, places an O in position 1
		if (boardPosition5 == 'X' && count < 0)
			{
				boardPosition1 = 'O';
				count++;
			}

		// checks for ways to block player
		if (boardPosition1 != '1')
		{
			if (boardPosition2 == 'X' && boardPosition1 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition3 == 'X' && boardPosition1 == 'X' && boardPosition2 == '2'&& count < 1)
			{
				boardPosition2 = 'O';
				count++;
			}
			if (boardPosition4 == 'X' && boardPosition1 == 'X' && boardPosition7 == '7'&& count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition1 == 'X' && boardPosition4 =='4'&& count < 1)
			{
				boardPosition4 = 'O';
				count++;
			}
			if (boardPosition5 == 'X' && boardPosition1 == 'X' && boardPosition9 == '9'&& count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition1 == 'X' && boardPosition5 == '5'&& count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}
		if (boardPosition2 != '2')
		{
			
			if (boardPosition5 == 'X' && boardPosition2 == 'X' && boardPosition8 == '8'&& count < 1)
			{
				boardPosition8 = 'O';
				count++;
			}
			if (boardPosition8 == 'X' && boardPosition2 == 'X' && boardPosition5 == '5'&& count < 1)
			{
				boardPosition5 = '5';
				count++;
			}
			if (boardPosition1 == 'X' && boardPosition2 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition2 == 'X' && boardPosition3 == 'X' && boardPosition1 == '1' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}
		

		if (boardPosition3 != '3')
		{
			if (boardPosition3 == 'X' && boardPosition7 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition5 == 'X' && boardPosition3 == 'X' && boardPosition7 == '7' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition6 == 'X' && boardPosition3 == 'X' && boardPosition9 == '9' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition3 == 'X' && boardPosition9 == 'X' && boardPosition6 == '6' && count < 1)
			{
				boardPosition6 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition4 != '4')
		{
			if (boardPosition4 == 'X' && boardPosition5 == 'X' && boardPosition6 == '6' && count < 1)
			{
				boardPosition6 = 'O';
				count++;
			}
			if (boardPosition4 == 'X' && boardPosition6 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition4 == 'X' && boardPosition1 == 'X' && boardPosition7 == '7' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition4 == 'X' && boardPosition7 == 'X' && boardPosition1 == '1' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}
			

			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition5 == 'X')
		{
			if (boardPosition4 == 'X' && boardPosition5 == 'X' && boardPosition6 == '6' && count < 1)
			{
				boardPosition6 = 'O';
				count++;
			}
			if (boardPosition5 == 'X' && boardPosition6 == 'X' && boardPosition4 == '4' && count < 1)
			{
				boardPosition4 = 'O';
				count++;
			}
			if (boardPosition8 == 'X' && boardPosition5 == 'X' && boardPosition2 == '2' && count < 1)
			{
				boardPosition2 = 'O';
				count++;
			}
			if (boardPosition2 == 'X' && boardPosition5 == 'X' && boardPosition8 == '8' && count < 1)
			{
				boardPosition8 = 'O';
				count++;
			}
			if (boardPosition3 == 'X' && boardPosition5 == 'X' && boardPosition7 == '7' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition5 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition1 == 'X' && boardPosition5 == 'X' && boardPosition9 == '9' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition5 == 'X' && boardPosition1 == '1' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}

			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition6 == 'X')
		{
			if (boardPosition3 == 'X' && boardPosition6 == 'X' && boardPosition9 == '9' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition6 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition4 == 'X' && boardPosition6 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition5 == 'X' && boardPosition6 == 'X' && boardPosition4 == '4' && count < 1)
			{
				boardPosition4 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition7 == 'X')
		{
			if (boardPosition7 == 'X' && boardPosition4 == 'X' && boardPosition1 == '1' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition1 == 'X' && boardPosition4 == '4' && count < 1)
			{
				boardPosition4 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition5 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition3 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition8 == 'X' && boardPosition9 == '9' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition7 == 'X' && boardPosition9 == 'X' && boardPosition8 == '8' && count < 1)
			{
				boardPosition8 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition8 == 'X')
		{
			if (boardPosition7 == 'X' && boardPosition8 == 'X' && boardPosition9 == '9' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition8 == 'X' && boardPosition7 == '7' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition8 == 'X' && boardPosition2 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition8 == 'X' && boardPosition5 == 'X' && boardPosition2 == '2' && count < 1)
			{
				boardPosition2 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition9 == 'X')
		{
			if (boardPosition9 == 'X' && boardPosition3 == 'X' && boardPosition6 == '6' && count < 1)
			{
				boardPosition6 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition6 == 'X' && boardPosition3 == '3' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition1 == 'X' && boardPosition5 == '5' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition5 == 'X' && boardPosition1 == '5' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition7 == 'X' && boardPosition8 == '8' && count < 1)
			{
				boardPosition8 = 'O';
				count++;
			}
			if (boardPosition9 == 'X' && boardPosition8 == 'X' && boardPosition7 == '7' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition5 != 'X' && boardPosition5 != 'O' && count < 1)
			{
				boardPosition5 = 'O';
				count++;
			}
		}

		if (boardPosition5 == 'O')
		{
			if (boardPosition1 != 'X' && boardPosition1 != 'O' && count < 1)
			{
				boardPosition1 = 'O';
				count++;
			}
			if (boardPosition2 != 'X' && boardPosition2 != 'O' && count < 1)
			{
				boardPosition2 = 'O';
				count++;
			}
			if (boardPosition3 != 'X' && boardPosition3 != 'O' && count < 1)
			{
				boardPosition3 = 'O';
				count++;
			}
			if (boardPosition4 != 'X' && boardPosition4 != 'O' && count < 1)
			{
				boardPosition4 = 'O';
				count++;
			}
			if (boardPosition6 != 'X' && boardPosition6 != 'O' && count < 1)
			{
				boardPosition6 = 'O';
				count++;
			}
			if (boardPosition7 != 'X' && boardPosition7 != 'O' && count < 1)
			{
				boardPosition7 = 'O';
				count++;
			}
			if (boardPosition8 != 'X' && boardPosition8 != 'O' && count < 1)
			{
				boardPosition8 = 'O';
				count++;
			}
			if (boardPosition9 != 'X' && boardPosition9 != 'O' && count < 1)
			{
				boardPosition9 = 'O';
				count++;
			}
		}

		// Variables for controlling the game ending and who wins
		GameOver = false;
		GameWin = true;

		// Game End checks
		if (boardPosition1 != '1')
		{
			if (boardPosition2 == boardPosition1 && boardPosition3 == boardPosition1)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition1 && boardPosition7 == boardPosition1)
			{
				GameOver = true;
			}

		}

		if (boardPosition5 != '5')
		{
			if (boardPosition1 == boardPosition5 && boardPosition9 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition2 == boardPosition5 && boardPosition8 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition5 && boardPosition6 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition3 == boardPosition5 && boardPosition7 == boardPosition5)
			{
				GameOver = true;
			}
		}

		if (boardPosition9 != '9')
		{
			if (boardPosition3 == boardPosition9 && boardPosition6 == boardPosition9)
			{
				GameOver = true;
			}
			if (boardPosition7 == boardPosition9 && boardPosition8 == boardPosition9)
			{
				GameOver = true;
			}
		}

		// Game Draw check
		if (boardPosition1 != '1' && boardPosition2 != '2' && boardPosition3 != '3' &&
			boardPosition4 != '4' && boardPosition5 != '5' && boardPosition6 != '6' &&
			boardPosition7 != '7' && boardPosition8 != '8' && boardPosition9 != '9')
		{
			GameOver = true;
			GameWin = false;
		}


		
		// Closing statements for game end
		if (GameOver)
		{
			if (GameWin)
			{
				cout << "Of course I would win. What other outcome could there be?" << endl;
			}
			else
			{
				cout << "It seems we've filled the board. How humiliating." << endl;
				return (0);
			}
			
			if (pTurn == 1)
			{
				pTurn = 2;

			}
			else 
			{
				pTurn = 1;
			}
		}

		// Draws the board after the AI does its turn
		cout << "  " << boardPosition1 << "  |  " << boardPosition2 << "  |  " << boardPosition3 << endl;
		cout << "_________________" << endl;
		cout << "  " << boardPosition4 << "  |  " << boardPosition5 << "  |  " << boardPosition6 << endl;
		cout << "_________________" << endl;
		cout << "  " << boardPosition7 << "  |  " << boardPosition8 << "  |  " << boardPosition9 << endl;

		// Returns Player turn so player can go
		return (pTurn = 1);
	}
	
};



// Start of main program
int main()
{
	// Local Variable Decleration
	bool valid;
	Board TicTacToeBoard;
	secPlayer Player2;
	int pTurn = 1;

	
	// Loop that checks user input for valid option and continues game till someone wins.
	do
	{

		if (pTurn = 2)
		{
			Player2.pMove2(pTurn);
		}
		
		cout << "Your move, Meatbag" << endl;

		do 
		{
			char pMove;
			cin >> pMove;
			valid = true;

			

		
			if (pMove == '1' && boardPosition1 == '1')
			{
				boardPosition1 = 'X';
			} 
			else if (pMove == '2' && boardPosition2 == '2')
			{
				boardPosition2 = 'X';
			} 
			else if (pMove == '3' && boardPosition3 == '3')
			{
				boardPosition3 = 'X';
			} 
			else if (pMove == '4' && boardPosition4 == '4')
			{
				boardPosition4 = 'X';
			}
			else if (pMove == '5' && boardPosition5 == '5') 
			{
				boardPosition5 = 'X';
			} 
			else if (pMove == '6' && boardPosition6 == '6') 
			{
				boardPosition6 = 'X';
			} 
			else if (pMove == '7' && boardPosition7 == '7') 
			{
				boardPosition7 = 'X';
			} 
			else if (pMove == '8' && boardPosition8 == '8') 
			{
				boardPosition8 = 'X';
			}
			else if (pMove == '9' && boardPosition9 == '9') 
			{
				boardPosition9 = 'X';
			} 
			else 
			{
				std::cout << "Invalid Move. Try again." << std::endl;
				valid = false;
			}
		}while (!valid);

			
		
		// Game End and Win variables
		GameOver = false;
		GameWin = true;

		// Game End and Win checks
		if (boardPosition1 != '1')
		{
			if (boardPosition2 == boardPosition1 && boardPosition3 == boardPosition1)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition1 && boardPosition7 == boardPosition1)
			{
				GameOver = true;
			}

		}

		if (boardPosition5 != '5')
		{
			if (boardPosition1 == boardPosition5 && boardPosition9 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition2 == boardPosition5 && boardPosition8 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition5 && boardPosition6 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition3 == boardPosition5 && boardPosition7 == boardPosition5)
			{
				GameOver = true;
			}
		}

		if (boardPosition9 != '9')
		{
			if (boardPosition3 == boardPosition9 && boardPosition6 == boardPosition9)
			{
				GameOver = true;
			}
			if (boardPosition7 == boardPosition9 && boardPosition8 == boardPosition9)
			{
				GameOver = true;
			}
		}

		if (boardPosition1 != '1' && boardPosition2 != '2' && boardPosition3 != '3' &&
			boardPosition4 != '4' && boardPosition5 != '5' && boardPosition6 != '6' &&
			boardPosition7 != '7' && boardPosition8 != '8' && boardPosition9 != '9')
		{
			GameOver = true;
			GameWin = false;
		}


		if (GameOver)
		{
			if (GameWin)
			{
				cout << "You win this round, Meatbag...." << endl;
			}

			TicTacToeBoard.drawBoard();

			cout << "It seems we've filled the board. How humiliating." << endl;
			
			if (pTurn == 1)
			{
				pTurn = 2;

			}
			else 
			{
				pTurn = 1;
			}
		}
		

	} while(!GameOver);

return (0);
}



